package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.AuthThreadLocal;
import com.itheima.common.JwtToken;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.dto.userDto.UserDto;
import com.itheima.pojo.vo.userVo.UserVo;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Boolean add(Map<String, String> map) {

        String phone = map.get("phone");

        String userId = IdUtil.getSnowflakeNextIdStr();
        Map<String,Object>map1=new HashMap<>();
        map1.put("token",userId);
        String token = JwtToken.jwt(map1);
        AuthThreadLocal.setAmount(token);
        UserDto user = new UserDto();
        user.setPhone(phone);
        user.setUserId(userId);
        int add = userMapper.add(user);
        return add > 0;

    }

    @Override
    public boolean delete(Map<String, String> map) {
        String phone = map.get("phone");
        int delete = userMapper.delete(phone);
        return delete > 0;
    }

    @Override
    public boolean login(Map<String, String> map) {
        String phone = map.get("phone");
        String code = map.get("code");
        UserVo user = userMapper.selectByPhone(phone);
        if (user==null){

        }
        return false;
    }

    @Override
    public UserVo selectByPhone(String phone) {
        UserVo user = userMapper.selectByPhone(phone);
        return user;
    }
}
